﻿
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports Microsoft.VisualBasic

Public Class Form1

	Dim n As Integer,
		isSaved As Boolean = False,
		fileName As String

	Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
	Handles TextBox1.TextChanged
		n = Len(TextBox1.Text)
		ToolStripStatusLabel1.Text = $"总字数：{n}"
	End Sub

	Private Sub New_Click(sender As Object, e As EventArgs) _
	Handles NewFileItem.Click
		TextBox1.Clear()
		Me.Text = "无标题 - 记事本"
		isSaved = False
	End Sub

	Private Sub Open_Click(sender As Object, e As EventArgs) _
	Handles OpenItem.Click
		OpenFileDialog1.ShowDialog()
	End Sub

	Private Sub OpenFile_OK(sender As Object, e As ComponentModel.CancelEventArgs) _
	Handles OpenFileDialog1.FileOk
		fileName = OpenFileDialog1.FileName
		TextBox1.Text = File.ReadAllText(fileName)
		isSaved = True
		Me.Text = $"{fileName} - 记事本"
	End Sub

	Private Sub Save_Click(sender As Object, e As EventArgs) _
	Handles SaveItem.Click
		If isSaved Then
			File.WriteAllText(fileName, TextBox1.Text)
		Else
			SaveFileDialog1.ShowDialog()
		End If
	End Sub

	Private Sub SaveAs_Click(sender As Object, e As EventArgs) _
	Handles SaveAsItem.Click
		SaveFileDialog1.ShowDialog()
	End Sub

	Private Sub SaveFile_OK(sender As Object, e As ComponentModel.CancelEventArgs) _
	Handles SaveFileDialog1.FileOk
		fileName = SaveFileDialog1.FileName
		File.WriteAllText(fileName, TextBox1.Text)
		isSaved = True
		Me.Text = $"{fileName} - 记事本"
	End Sub

	Private Sub Exit_Click(sender As Object, e As EventArgs) _
	Handles ExitItem.Click
		Close()
	End Sub

	Private Sub Undo_Click(sender As Object, e As EventArgs) _
	Handles UndoItem.Click
		TextBox1.Undo()
	End Sub

	Private Sub Cut_Click(sender As Object, e As EventArgs) _
	Handles CutItem.Click
		TextBox1.Cut()
	End Sub

	Private Sub Copy_Click(sender As Object, e As EventArgs) _
	Handles CopyItem.Click
		TextBox1.Copy()
	End Sub

	Private Sub Paste_Click(sender As Object, e As EventArgs) _
	Handles PasteItem.Click
		TextBox1.Paste()
	End Sub

	Private Sub Delete_Click(sender As Object, e As EventArgs) _
	Handles DeleteItem.Click
		TextBox1.Cut()
		Clipboard.Clear()
	End Sub

	Private Sub SelectAll_Click(sender As Object, e As EventArgs) _
	Handles SelectAllItem.Click
		TextBox1.SelectAll()
	End Sub

	Private Sub DateTime_Click(sender As Object, e As EventArgs) _
	Handles DateTimeItem.Click
		TextBox1.Paste(Now.ToString())
	End Sub

	Private Sub WordWrap_Click(sender As Object, e As EventArgs) _
	Handles WordWrapItem.Click
		If WordWrapItem.Checked Then
			TextBox1.WordWrap = True
			TextBox1.ScrollBars = ScrollBars.Vertical
			StatusStrip1.Hide()
			ToolStripItem.Enabled = False
		Else
			TextBox1.WordWrap = False
			TextBox1.ScrollBars = ScrollBars.Both
			StatusStrip1.Show()
			ToolStripItem.Enabled = True
		End If
	End Sub

	Private Sub Font_Click(sender As Object, e As EventArgs) _
	Handles FontItem.Click
		FontDialog1.ShowDialog()
		TextBox1.Font = FontDialog1.Font
	End Sub

	Private Sub ToolStrip_Click(sender As Object, e As EventArgs) _
	Handles ToolStripItem.Click
		If ToolStripItem.Checked Then
			StatusStrip1.Show()
		Else
			StatusStrip1.Hide()
		End If
	End Sub

	Private Sub About_Click(sender As Object, e As EventArgs) _
	Handles AboutItem.Click
		About.Show()
	End Sub

End Class
